package comWorld.action.blog;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.gjs.tools.Date.DateUtil;

import comWorld.blogComment.BlogComment;
import comWorld.blogComment.BlogCommentManage;
import comWorld.user.UserInfo;
import dataManage.tools.Sequence;

/**
 * Servlet implementation class AddComment
 */
@WebServlet("/action/blog/addComment")
public class AddComment extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddComment() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int blogId = Integer.valueOf(request.getParameter("blogId"));
		String content = request.getParameter("content");
		String replyId = request.getParameter("reply_id");
		UserInfo commenter = (UserInfo) request.getSession().getAttribute("visitorInfo");
		int userId = commenter.getId();
		String createTime = DateUtil.formatDate(new Date(), DateUtil.DATESTRFORMAT);
		int commentId = Sequence.queryNextVal("comment_id_sq");
		BlogComment comment = null;
		if (replyId == null || replyId.length() == 0) {
			comment = new BlogComment(blogId, userId, commentId, null, null, null, createTime, content, 0);
		} else {
			String[] replyInfo = replyId.split("_");
			int replyUserId = Integer.valueOf(replyInfo[0]);
			int replyComId = Integer.valueOf(replyInfo[1]);
			comment = new BlogComment(blogId, userId, commentId, null, null, null, createTime, content, replyComId);
		}
		BlogCommentManage.addBlogComment(comment);
		comment = BlogCommentManage.getBlogComment(blogId, commentId);
		response.getWriter().println(JSON.toJSONString(comment));
	}
}
